package com.ostrich.cloud.ostrichDistributionService.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ostrich.cloud.dto.DistributionDto;
import com.ostrich.cloud.ostrichDistributionService.infra.mapper.DeliveryOrderMapper;
import com.ostrich.cloud.ostrichDistributionService.infra.model.DeliveryOrder;
import com.ostrich.cloud.ostrichDistributionService.service.DeliveryOrderService;
import com.ostrich.cloud.param.DeliveryOrderparam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 配送订单表 服务实现类
 * </p>
 *
 * @author Tom
 * @since 2022-10-29
 */
@Service
@Slf4j
public class DeliveryOrderServiceImpl extends ServiceImpl<DeliveryOrderMapper, DeliveryOrder> implements DeliveryOrderService {
    @Resource
    private DeliveryOrderMapper deliveryOrderMapper;
    @Override
    public boolean storeAdd(DeliveryOrder deliveryOrder) {
        //1.查询任务是否存在2.修改状态
        DeliveryOrder deliveryOrder1 = (DeliveryOrder) deliveryOrderMapper.selectById(deliveryOrder);
        if(deliveryOrder1==null){
            log.info("该配送任务不存在{}",deliveryOrder.getDeliveryId());
            return false;
        }else {
            int i = deliveryOrderMapper.updateById(deliveryOrder);
            if(i<=0){
                log.info("修改失败");
                return false;
            }else {
                return true;
            }
        }
    }

    @Override
    public Integer orderAdd(DeliveryOrder deliveryOrder) {
        //1.检测数据库是否重复2.添加任务3.返回主键id
        Long deliveryNumber = deliveryOrder.getDeliveryNumber();
        QueryWrapper<DeliveryOrder> qw=new QueryWrapper<>();
        qw.eq("delivery_number",deliveryNumber);
        com.ostrich.cloud.ostrichDistributionService.infra.model.DeliveryOrder deliveryOrder1 = deliveryOrderMapper.selectOne(qw);
        if(deliveryOrder1!=null){
            log.info("该任务已存在");
            return null;
        }else {
            int insert = deliveryOrderMapper.insert(deliveryOrder);
            if(insert<=0){
                log.info("添加失败");
                return null;
            }else {
                return deliveryOrder.getDeliveryId();
            }
        }
    }

    @Override
    public DistributionDto getDeliveryPage(Integer bid, long currentPage, long size) {
        IPage<DeliveryOrder> page=new Page<>();
        page.setSize(size);
        page.setCurrent(currentPage);
        QueryWrapper<DeliveryOrder> qw=new QueryWrapper<>();
        qw.eq("businessman_id",bid);
        IPage<DeliveryOrder> iPage = deliveryOrderMapper.selectPage(page, qw);
        log.info("{}",iPage);
        DistributionDto distributionDto = new DistributionDto();
        List<DeliveryOrderparam> list = new ArrayList();
        distributionDto.setDeliveryOrderList(BeanUtil.copyToList(iPage.getRecords(),DeliveryOrderparam.class));
        distributionDto.setCurrentPage(iPage.getCurrent());
        distributionDto.setSize(iPage.getSize());
        distributionDto.setTotalNumber(iPage.getTotal());
        distributionDto.setTotalPage(iPage.getPages());
        return distributionDto;
    }

    @Override
    public boolean finishedDelivery(Integer did, Date finishedTime) {
        DeliveryOrder deliveryOrder = new DeliveryOrder();
        deliveryOrder.setDeliveryId(did);
        deliveryOrder.setFinishTime(finishedTime);
        deliveryOrder.setStatus("FINISHED");
        int i = deliveryOrderMapper.updateById(deliveryOrder);
        log.info("结束订单{}",i);
        return i>0?true:false;
    }

    @Override
    public DeliveryOrderparam getOne(Integer did) {
        DeliveryOrder deliveryOrder = deliveryOrderMapper.selectById(did);
        DeliveryOrderparam deliveryOrderparam=new DeliveryOrderparam();
        BeanUtil.copyProperties(deliveryOrder,deliveryOrderparam);
        log.info("查询配送订单：{}",deliveryOrder);
        if (deliveryOrderparam==null){
            throw new RuntimeException("该订单不存在");
        }
        return deliveryOrderparam;
    }
}
